<template>
  <div>
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <el-row>
        <!-- <el-col :span="8">
          <el-form-item label="单据编号" prop="number">
            <el-input v-model="form.number" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="本次结算金额" prop="thisSettleAmount">
            <el-input v-model="form.thisSettleAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="本次支付金额" prop="thisPayAmount">
            <el-input v-model="form.thisPayAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付日期" prop="payTime">
            <el-date-picker v-model="form.payTime" :disabled="disableForm" type="date" placeholder="选择日期" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付方式" prop="payType">
            <el-select v-model="form.payType" :disabled="disableForm" style="width: 100%;" placeholder="请选择支付方式">
              <el-option v-for="item in payTypes" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付单位" prop="payUnit">
            <el-input v-model="form.payUnit" :disabled="disableForm" />

            <!-- <el-select v-model="form.payUnit" :disabled="disableForm" style="width: 100%;" placeholder="请选择支付单位">
              <el-option v-for="item in payUnits" :key="item.name" :label="item.name" :value="item.value" />
            </el-select> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="2" :disabled="disableForm" />
      </el-form-item>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { addOtherContractSettleInfo, updateOtherContractSettleInfo, getOtherContractSettleInfoDetail } from '@/api/contractManage'
import { payTypes, payUnits } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddOtherContractInfoSettleInfo',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    relateData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      payTypes,
      payUnits,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        // number: '',
        thisSettleAmount: '',
        thisPayAmount: '',
        payTime: '',
        payType: '',
        payUnit: '',
        remark: ''
      },
      fileList: [
      ],
      rules: {
        thisSettleAmount: [
          { required: true, message: '请输入本次结算金额', trigger: 'change' }
        ],
        thisPayAmount: [
          { required: true, message: '请输入本次支付金额', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
        this.$refs.form.resetFields()
        this.fileList = []
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.businessId) {
          this.getDocDetail(newData.businessId)
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    getDocDetail(id) {
      getOtherContractSettleInfoDetail({ otherContractSettleInfoId: id }).then(res => {
        const { lstAttachment, otherContractSettleInfo } = res.data || {}
        Object.keys(this.form).forEach(key => {
          const source = otherContractSettleInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          const params = {
            otherContractSettleInfo: {
              ...formData,
              otherContractInfoId: this.relateData.id,
              thisSettleAmount: Number(formData.thisSettleAmount),
              thisPayAmount: Number(formData.thisPayAmount),
              ... this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addOtherContractSettleInfo : updateOtherContractSettleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
